--UserDisplay
--create by zouyb
--玩家显示对象

require "game/ui/module/Avatar"
local direction = require "game/ui/base/Direction";

local STATE_IDLE = 0;
local STATE_RUN  = 1;


--UserDisplay继承自Node
UserDisplay = class("UserDisplay", function() 
    return cc.Node:create();
end);

function UserDisplay.create(name, job)
    return UserDisplay.new(name, job);
end    

--构造函数
function UserDisplay:ctor(name, job)
    
    --显示外观
    self.avatar = Avatar.create("anim/me_run.csb");
    self.avatar:setScale(0.7 * UIMgr.getBuildingScale());
    self:addChild(self.avatar); 
		
	--设置行走速度
	self.speed = 200;
	
	self:gotoIdle();
end

--设置方向
function UserDisplay:setDir(dir)
    self.avatar:setDir(dir);
end

--获取方向
function UserDisplay:getDir(dir)
    return self.avatar:getDir(dir);
end

--设置速度
function UserDisplay:setSpeed(speed)
    self.speed = speed;
end

--获取速度
function UserDisplay:getSpeed(speed)
    return self.speed;
end

--行走
function UserDisplay:gotoRun()
    self.avatar:play("run");
    self.state = STATE_RUN;
end

--静止
function UserDisplay:gotoIdle()
    self.avatar:play("idle");
    self.state = STATE_IDLE;
end

function UserDisplay:moveTo(x, y)
    local curPos = cc.p(self:getPosition());

    --计算距离
    local dis = cc.pGetDistance(curPos, cc.p(x,y));
    
    --计算行走时间
    local time = dis / self:getSpeed();
    
    local function onStopMove()
        self:gotoIdle();
        EventMgr.fire(event.USER_STOP_MOVE);
    end
       
    --开始走
    self:stopAllActions();  
    self:runAction(cc.Sequence:create(cc.MoveTo:create(time, cc.p(x, y)), cc.CallFunc:create(onStopMove)));
    self:gotoRun();
     
    --改变方向
    self:setDir(direction.getDir(x-curPos.x, y-curPos.y));
end

